%题干中的x,y如下，若想输入其他的，可自行修改.
x = linspace(0, 10, 21);
y = [2.9, 2.7, 4.8, 5.3, 7.1, 7.6, 7.7, 7.6, 9.4, 9.0, 9.6, 10.0, 10.2, 9.7, 8.3, 8.4, 9.0, 8.3, 6.6, 6.7, 4.1]; 

%创建正规方程组矩阵的系数
x_const = x.^0;
x_quad = x.^2;
x_cubic = x.^3;
x_quar = x.^4;

g11 = sum(x_const);
g12 = sum(x);
g13 = sum(x_quad);
g23 = sum(x_cubic);
g33 = sum(x_quar);

G = [g11 g12 g13;
     g12 g13 g23;
     g13 g23 g33;]; %正规方程组的系数矩阵

%创建正规方程组等式右端项
c1 = sum(y);
c2 = dot(x, y);
c3 = dot(x_quad, y);

c = [c1; c2; c3];

a = G \ c;  %解正规方程组

%作图
hold on  
fplot(@(x) a(1) + a(2)*x + a(3)*x.^2, [x(1), x(end)], 'b');  
plot(x, y, 'ro');  
hold off  